package hapi.web;

class ParamsParser {
	
	static var params = server.Web.getParams();
	
	public static function parseString(param:String, ?maxLength:Int):String {
		var value = params.get(param);
		if (value == null) value = "";
		else if ( maxLength != null) {
			value = value.substr(0, maxLength);
		}
		return value;
	}
	
	public static function parseInt(param:String):Int {
		return Std.parseInt(params.get(param));
	}
	
	public static function parseObject(param:String):Dynamic {
		var result = null;
		var offset = param.length + 1;
		for ( p in params.keys() ) {
			if ( StringTools.startsWith(p, param)) {
				var field = p.substr( offset );
				if ( field != "" ) {
					if ( result == null ) result = {};
					Reflect.setField(result, field, params.get(p));
				}
			}
		}
		return result;
	}
}